package com.atguigu.day06;

import java.util.Arrays;
import java.util.Scanner;

public class Test03 {
    public static void main(String[] args) {
        int[] intArr = {2, 4, 3, 5, 8, 1, 6, 9, 7};
        char[] charArr = {'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd'};
        double[] doubleArr = {3.14, 6.16, 4.66, 10.20};
        MyArrays m1 = new MyArrays();
        System.out.println("***************原来的三组数组是：");
        System.out.println("int数组：" + Arrays.toString(intArr));
        System.out.println("char数组：" + Arrays.toString(charArr));
        System.out.println("double数组：" + Arrays.toString(doubleArr));
        System.out.println("***************不排序查询的话：");
        System.out.println("查询0：" + m1.binarySearch(intArr, 0));
        System.out.println("查询4：" + m1.binarySearch(intArr, 4));
        System.out.println("查询A：" + m1.binarySearch(charArr, 'A'));
        System.out.println("查询e：" + m1.binarySearch(charArr, 'e'));
        System.out.println("查询3.17：" + m1.binarySearch(doubleArr, 3.17));
        System.out.println("查询6.16：" + m1.binarySearch(doubleArr, 6.16));
        System.out.println("***************抄一下前面四个元素：");
        System.out.println("int数组：" + Arrays.toString(m1.copy(intArr, 4)));
        System.out.println("char数组：" + Arrays.toString(m1.copy(charArr, 4)));
        System.out.println("double数组：" + Arrays.toString(m1.copy(doubleArr, 4)));
        System.out.println("***************排一下序：");
        m1.sort(intArr);
        System.out.println("int数组：" + Arrays.toString(intArr));
        m1.sort(charArr);
        System.out.println("char数组：" + Arrays.toString(charArr));
        m1.sort(doubleArr);
        System.out.println("double数组：" + Arrays.toString(doubleArr));
        System.out.println("***************排序后再查询：");
        System.out.println("int数组：" + m1.binarySearch(intArr, 4));
        System.out.println("char数组：" + m1.binarySearch(charArr, 'e'));
        System.out.println("double数组：" + m1.binarySearch(doubleArr, 6.16));
        System.out.println("***************再抄一次");
        System.out.println("int数组：" + Arrays.toString(m1.copy(intArr, 4)));
        System.out.println("char数组：" + Arrays.toString(m1.copy(charArr, 4)));
        System.out.println("double数组：" + Arrays.toString(m1.copy(doubleArr, 4)));
    }
}

class MyArrays {
    int binarySearch(int[] arr, int value) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int middle = (left + right) / 2;
            if (value > arr[middle]) {
                left = middle + 1;
            } else if (arr[middle] > value)
                right = middle - 1;
            else
                return middle;

        }
        return -(left + 1);
    }

    int binarySearch(char[] arr, char value) {
        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int middle = (left + right) / 2;
            if (value > arr[middle]) {
                left = middle + 1;
            } else if (arr[middle] > value)
                right = middle - 1;
            else
                return middle;

        }
        return -(left + 1);
    }

    int binarySearch(double[] arr, double value) {
        int left = 0;
        int right = arr.length - 1;

        while (left <= right) {
            int middle = (left + right) / 2;
            if (value > arr[middle]) {
                left = middle + 1;
            } else if (arr[middle] > value)
                right = middle - 1;
            else
                return middle;

        }
        return -(left + 1);
    }

    int[] copy(int[] arr, int length) {
        int[] subarr = new int[length];
        for (int i = 0; i < length; i++) {
            subarr[i] = arr[i];
        }
        return subarr;
    }

    double[] copy(double[] arr, int length) {
        double[] subarr = new double[length];
        for (int i = 0; i < length; i++) {
            subarr[i] = arr[i];
        }
        return subarr;
    }

    char[] copy(char[] arr, int length) {
        char[] subarr = new char[length];
        for (int i = 0; i < length; i++) {
            subarr[i] = arr[i];
        }
        return subarr;
    }

    void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    void sort(char[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    char temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    void sort(double[] arr) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    double temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}
